package com.tan.androidtest.customview.measure;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 * 测试重写 onMeasure() 来修改已有的 View 的尺寸
 * 做一个 边长根据父布局的短边来确定的正方形
 */

public class DemoView4 extends AppCompatImageView {


    public DemoView4(Context context) {
        super(context);
    }

    public DemoView4(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoView4(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //1.重写onMeasure方法,调用super.onMeasure方法来触发原来的自身的测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //2.获取原来的尺寸
        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();

        //3.计算出新的尺寸,并使用setMeasuredDimension方法来保存

        if (measureWidth > measureHeight) {
            measureWidth = measureHeight;
        } else if (measureWidth < measureHeight) {
            measureHeight = measureWidth;
        }
        setMeasuredDimension(measureWidth, measureHeight);
    }


}
